package com.voute.Presenter.Impl;

import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.CreateFeedResponseBean;
import com.voute.Model.Bean.UxTagBean;
import com.voute.Model.IFeedModel;
import com.voute.Model.IUxTagModel;
import com.voute.Model.Impl.FeedModelImpl;
import com.voute.Model.Impl.UXTagModelImpl;
import com.voute.Presenter.ICreateFeedPresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.PreferenceUtils;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.IPublishFeedView;

import java.util.HashMap;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by liucxu on 2017/1/18.
 */

public class CreateFeedPresenterImpl implements ICreateFeedPresenter {

    private IPublishFeedView create_view;
    private IUxTagModel uxModel;
    private IFeedModel feedModel;

    public CreateFeedPresenterImpl(IPublishFeedView view) {
        if (view != null) {
            create_view = view;
            uxModel = new UXTagModelImpl();
            feedModel = new FeedModelImpl();
        }
    }

    @Override
    public void createFeed(final String title, final String pic, final String description) {
        create_view.showLoading();
        uxModel.getUxTag().flatMap(new Func1<UxTagBean, Observable<CreateFeedResponseBean>>() {
            @Override
            public Observable<CreateFeedResponseBean> call(UxTagBean uxTagBean) {
                HashMap<String, String> values = new HashMap<String, String>();
                values.put("title", title);
                values.put("uxtag",uxTagBean.getData().getUxtag());
                values.put("pic", pic);
                values.put("description", description);
                values.put("latitude", PreferenceUtils.GetLatitude());
                values.put("longitude", PreferenceUtils.GetLongtitude());
                return feedModel.createFeed(values);
            }
        }).compose(TransformUtils.<CreateFeedResponseBean>defaultSchedulers()).subscribe(new CommonSubscriber<CreateFeedResponseBean>() {
            @Override
            public void onSuccess(CreateFeedResponseBean response) {
                create_view.hideLoading();
                create_view.createFeedSuccess(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                create_view.hideLoading();
                create_view.showError(response.getMsg());
            }
        });
    }
}
